package pei.lei.lostandfound.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import pei.lei.lostandfound.base.utils.KeyUtil;
import pei.lei.lostandfound.base.utils.ResultUtil;
import pei.lei.lostandfound.base.dto.ResultDTO;
import pei.lei.lostandfound.base.page.PageList;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import pei.lei.lostandfound.base.utils.RoleUtils;
import pei.lei.lostandfound.entity.UserInfo;
import pei.lei.lostandfound.service.UserInfoService;
import pei.lei.lostandfound.service.UserService;
import pei.lei.lostandfound.entity.User;

import java.util.List;


/**
 *
 * @author Zhang
 * @since 2021-01-18
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
@Slf4j
public class UserController {

        @Autowired
        private UserService userService;

        @Autowired
        private UserInfoService userInfoService;

        @PostMapping("/test")
        public ResultDTO test(@RequestBody User user){
/*            System.out.println("--------------------");
            System.out.println(user);*/
            return ResultUtil.Success(user);
        }


        @PostMapping("/register")
        @ApiOperation(value = "用户注册")
        /*@ApiImplicitParams({
                @ApiImplicitParam(name = "userId", dataType = "String", required = true, value = "用户账号"),
                @ApiImplicitParam(name = "password", dataType = "String", required = true, value = "用户密码")
        })*/
        public ResultDTO register(@RequestBody User user)throws Exception{
            User testUser = new User();
            testUser.setUserId(user.getUserId());
            System.out.println(user);
            if (user.getPassword().isEmpty()) {
                return ResultUtil.Error("500","密码为空");
            }
            try {
                List<User> userList = userService.findByParams(testUser);
                if (!userList.isEmpty()) {
                    return ResultUtil.Error("500","该用户名已被注册");
                }
                user.setRoleName(RoleUtils.USER_ROLE);
                userService.save(user);
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(user.getUserId());
                userInfo.setUserName("user-"+ KeyUtil.getUniqueKey());
                userInfoService.save(userInfo);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            user.setPassword(null);
            return ResultUtil.Success(user);
        }

        @PostMapping("/login")
        @ApiOperation(value = "登陆,用户和管理员用同一个登陆接口")
        /*@ApiImplicitParams({
                @ApiImplicitParam(name = "userId", dataType = "String", required = true, value = "用户账号"),
                @ApiImplicitParam(name = "password", dataType = "String", required = true, value = "用户密码")
                })*/
        public ResultDTO login(@RequestBody User user)throws Exception{
            if (user.getPassword().isEmpty()) {
                return ResultUtil.Error("500","密码为空");
            }
            if (user.getUserId().isEmpty()) {
                return ResultUtil.Error("500","用户名为空");
            }

            try {
                User findNameUser = new User();
                findNameUser.setUserId(user.getUserId());
                if (userService.findByParams(findNameUser).isEmpty()) {
                    return ResultUtil.Error("500","该用户不存在");
                }
                List<User> userList = userService.findByParams(user);
                if (userList.isEmpty()) {
                    return ResultUtil.Error("500","密码错误");
                }
                user = userList.get(0);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            user.setPassword("");
            return ResultUtil.Success(user);
        }

        @PostMapping("/insertUser")
        @ApiOperation(value = "插入一条数据")
        //@ApiImplicitParams()
        public ResultDTO insertOne( @RequestBody User user)throws Exception{
            try {
                userService.save(user);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(user);
        }

        @PostMapping("/updateById")
        @ApiOperation(value = "按id修改数据")
        //@ApiImplicitParams()
        public ResultDTO updateById( @RequestBody User user)throws Exception{
            try {
                userService.update(user);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(userService.get(user.getUserId()));

        }

        @GetMapping("/deleteById/{id}")
        @ApiOperation("按id删除数据")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "id", dataType = "string", required = true, value = "实体类id"),

        })
        public ResultDTO deleteById(@PathVariable("id") String id)throws Exception{
            User oldUser = null;
            try {
                oldUser = userService.get(id);
                userService.deleteById(id);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(oldUser);
        }

        @PostMapping("/getUserPage")
        @ApiOperation("按条件查询，分页")
        @ApiImplicitParams({
                @ApiImplicitParam( name = "pageNum", dataType = "Integer", defaultValue = "1", required = false, value = "页码"),
                @ApiImplicitParam( name = "pageSize", dataType = "Integer", defaultValue = "10", required = false, value = "每页容量")
        })
        public ResultDTO selectEntityPage(@RequestBody  User user,
                                          @RequestParam(value = "pageNum", defaultValue = "1", required=false ) int pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10", required=false )int pageSize) throws Exception{
            PageList<User> pageData = null;
            try {
                pageData = userService.findPageData(user, pageNum, pageSize);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(pageData);

        }

        @PostMapping("/getUser")
        @ApiOperation("按条件查询，不分页")
        //@ApiImplicitParams()
        public ResultDTO selectEntityPage( @RequestBody User user)throws Exception{
            List<User> userList = null;
            try {
                userList = userService.findByParams(user);
            } catch (Exception e) {
                return ResultUtil.Error("500",e.getMessage());
            }
            return ResultUtil.Success(userList);
        }


}
